package f.h.dragon.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类 TODO -O
 * 
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2011/10/25
 * @since 1.0
 */
public final class DateUtil {
	
	/**
	 * 判断d1与d2是否属于同一type
	 * 
	 * @param d1
	 * @param d2
	 * @param type see{@link Type}}
	 * @return
	 */
	public final static boolean isSame(Date d1, Date d2, Type type){
		if(d1 != null && d2 != null){
			if(type == null || type.value() == Type.SECOND.value()){
				return d1.getTime() == d2.getTime();
			}
			//date to calendar
			Calendar c1 = Calendar.getInstance();
			c1.setTimeInMillis(d1.getTime());
			Calendar c2 = Calendar.getInstance();
			c2.setTimeInMillis(d2.getTime());
			int rs = 0;
			rs = rs | (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) ? 1 : 0);
			rs = rs | (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) ? 2 : 0);
			rs = rs | (c1.get(Calendar.DATE) == c2.get(Calendar.DATE) ? 4 : 0);
			rs = rs | (c1.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY) ? 8 : 0);
			rs = rs | (c1.get(Calendar.MINUTE) == c2.get(Calendar.MINUTE) ? 16 : 0);
			return (rs & type.value()) == type.value();
		}
		return false;
	}
	
	/**
	 * 获取一天最小的时间
	 * 
	 * @param d
	 * @return
	 */
	public final static Date getMinDayTime(Date d){
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 0, 0, 0);
		return c.getTime();
	}
	
	/**
	 * 获取一天最大的时间
	 * 
	 * @param d
	 * @return
	 */
	public final static Date getMaxDayTime(Date d){
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 23, 59, 59);
		return c.getTime();
	}
	
	/**
	 * 日期运算
	 * 
	 * @param d
	 * @param type see {@link Type}}
	 * @return
	 */
	public final static Date add(Date d, int v,Type type){
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int field = 0; 
		if(type.value() == Type.DAY.value()){
			field = Calendar.DATE;
		}else if(type.value() == Type.YEAR.value()){
			field = Calendar.YEAR;
		}else if(type.value() == Type.MONTH.value()){
			field = Calendar.MONTH;
		}else if(type.value() == Type.HOUR.value()){
			field = Calendar.HOUR_OF_DAY;
		}else if(type.value() == Type.MINUTE.value()){
			field = Calendar.MINUTE;
		}else if(type.value() == Type.SECOND.value()){
			field = Calendar.SECOND;
		}
		c.add(field, v);
		d.setTime(c.getTimeInMillis());
		return d;
	}
	
	/**
	 * 比较的日期类型
	 * 
	 * @author Wenlong Meng(wenlong.meng@gmail.com)
	 * @version 1.0 at 2011/10/25
	 * @since 1.0
	 */
	public static enum Type{
		YEAR{
			public int value(){
				return 1;
			}
		},
		MONTH{
			public int value(){
				return 3;
			}
		},
		DAY{
			public int value(){
				return 7;
			}
		},
		HOUR{
			public int value(){
				return 15;
			}
		},
		MINUTE{
			public int value(){
				return 31;
			}
		},
		SECOND{
			public int value(){
				return 63;
			}
		};
		/**
		 * 默认状态-1
		 * 
		 * @return
		 */
		public int value(){
			return 0;
		}
	}
	
	//Constants
	/**
	 * 时差：毫秒
	 */
	public final static long timeZoneOff = Calendar.getInstance().getTimeZone().getRawOffset();
	/**
	 * second In milliseconds
	 */
	public final static long SECOND_IN_MILLIS = 1000L;
	/**
	 * minute In milliseconds
	 */
	public final static long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60L;
	/**
	 * hour In milliseconds
	 */
	public final static long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
	/**
	 * day In milliseconds
	 */
	public final static long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
	/**
	 * date types
	 */
	public final static int[] c_date_types = {Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND};
	
	/**
	 * testing...
	 * 
	 * @param args
	 */
	@SuppressWarnings("deprecation")
	public static void main(String[] args){
		System.out.println(getMinDayTime(new Date()));
		System.out.println(getMaxDayTime(new Date()));
		System.out.println(new Date(new Date().getTime() - 100000l));
		System.out.println(isSame(new Date(2011, 10, 24, 7, 59), new Date(2011, 10, 24, 23, 59), Type.DAY));
	}

}
